package bar.dark.lee.setting.viewtype;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import bar.dark.lee.R;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.RadioDialogActivity;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.select.SelectActivity;
import bar.dark.lee.select.SelectCalenderActivity;
import bar.dark.lee.select.search.SearchActivity;
import bar.dark.lee.widget.WidgetArrayList;
import bar.dark.lee.widget.WidgetGetSqlQuery;

public class WidgetDialogActivity extends Activity
{
	public static String RADIOSELECT = "radioselect";
	public static String REQUEST_CODE = "request_code";
	public static String WIDGET_TYPE_KEY = "widget_type_key";
	
	private DBSQLite sql;
	
	private View viewNormal;
	private View viewBankbook;
	private View viewCheckCard;
	private View viewCreditCard;
	private Button btnCancel;

	private Intent intent;
	
	private ViewTypeList widgetList;
	private ArrayList<String> bankList;
	private ArrayList<String> checkList;
	private ArrayList<String> creditList;
	
	private String name;
	private int request_key;
	private String widget_key;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
	    setContentView(R.layout.widget_dialog_activity);
	    
	    sql = DBSQLite.getInstance(this);
	    
	    name = getIntent().getStringExtra(RADIOSELECT);
	    request_key = getIntent().getIntExtra(REQUEST_CODE, -1);
	    
	    setFindView();
	    setDataSetting();
	    setClickListener();
	}

	public void setFindView() 
	{
		viewNormal = findViewById(R.id.widgetDialog_normal);
		viewBankbook = findViewById(R.id.widgetDialog_bankbook);
		viewCheckCard = findViewById(R.id.widgetDialog_checkcard);
		viewCreditCard = findViewById(R.id.widgetDialog_creditcard);
		btnCancel = (Button) findViewById(R.id.widgetSelectDialog_cancel);
	}

	public void setDataSetting() 
	{
		widget_key = "0";
		
		widgetList = new ViewTypeList();
		
		sql.selectMapTile(this, "SELECT name FROM ( SELECT b.name name FROM bankbook b, bankbook_detail d WHERE b.id = d.linkid and count > 0 ORDER BY count) " +
				"UNION ALL " +
				"SELECT name FROM ( SELECT b.name name FROM bankbook b, bankbook_detail d WHERE b.id = d.linkid and count < 1 ORDER BY count);", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				bankList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("name");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					bankList.add(kind);
					c.moveToNext();
				}
				
				if (bankList.size() < 1)
				{
					String memo = "없음";
					bankList.add(memo);
				}
			}
		});
		
		sql.selectMapTile(this, "SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM check_card b, checkcard_detail d " +
								"WHERE b.id = d.linkid and d.count > 0 ORDER BY d.count) " +
								"UNION ALL " +
								"SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM check_card b, checkcard_detail d " +
								"WHERE b.id = d.linkid and d.count < 1 ORDER BY d.count);", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				checkList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("name");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					checkList.add(kind);
					c.moveToNext();
				}
				
				if (checkList.size() < 1)
				{
					String memo = "없음";
					checkList.add(memo);
				}
			}
		});
		
		sql.selectMapTile(this, "SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM credit_card b, creditcard_detail d " +
								"WHERE b.id = d.linkid and d.count > 0 ORDER BY d.count) " +
								"UNION ALL " +
								"SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM credit_card b, creditcard_detail d " +
								"WHERE b.id = d.linkid and d.count < 1 ORDER BY d.count);", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				creditList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("name");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					creditList.add(kind);
					c.moveToNext();
				}
				
				if (creditList.size() < 1)
				{
					String memo = "없음";
					creditList.add(memo);
				}
			}
		});
	}

	public void setClickListener() 
	{
		
		viewNormal.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				widget_key = "-1";
				Intent intent = new Intent(WidgetDialogActivity.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, widgetList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, name);
				startActivityForResult(intent, request_key);
			}
		});
		
		viewBankbook.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				widget_key = WidgetGetSqlQuery.BANKBOOK;
				Intent intent = new Intent(WidgetDialogActivity.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, bankList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, name);
				startActivityForResult(intent, request_key);
			}
		});
		
		viewCheckCard.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				widget_key = WidgetGetSqlQuery.CHECKCARD;
				Intent intent = new Intent(WidgetDialogActivity.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, checkList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, name);
				startActivityForResult(intent, request_key);
			}
		});
		
		viewCreditCard.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				widget_key = WidgetGetSqlQuery.CREDITCARD;
				Intent intent = new Intent(WidgetDialogActivity.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, creditList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, name);
				startActivityForResult(intent, request_key);
			}
		});
		
		btnCancel.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				finish();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode == Activity.RESULT_OK)
		{
			String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
			
			Intent intent = new Intent();
			intent.putExtra(RadioDialogActivity.RADIOSTRING, selectData);
			intent.putExtra(WIDGET_TYPE_KEY, widget_key);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
		else
		{
			finish();
		}
	}
}
